###################################################################
# Project:	OpenDigiTV
# Page:		http://www.opendigitv.fi
# Modulename:	OpenDigiTV - MovieTrailers
# Language:	English / Python
# Version:	0.0.1 b1
# Description:
#		This module will parse google's movie trailer list
#		and will download the files. Creates index.xml.
#		Includes cache.
#
# This version is quick-and-dirty as proof-of-concept can be.
# The real version is coming soon! :)
#
# (C) 2006 	Juhapekka Piiroinen 
# 	   	juhapekka@jpii.fi
#		http://www.jpii.fi
#
# license: GNU/GPL (OpenSource)
###################################################################
import httplib
import urllib
from pyparsing import *
import os
import sys
from time import *

proxies = {'http': 'http://proxy3.cc.jyu.fi:8080'}
opener = urllib.FancyURLopener(proxies)
	
print "------------------------------------------"
print "OpenDigiTV - MovieTrailers"
print " (C) 2006 Juhapekka Piiroinen"
print " GNU/GPL"
print "------------------------------------------"

def download(file,net):
        file = open(file,"w")
	f = opener.open(net)
	file.write(f.read())
	file.close()
def createIndex(filename,thumb,title):
	file = open("%s.odtv" % filename,"w")
	file.write("%s;%s;%s" % (title,filename,thumb))
	file.close()
def downloadFile(local,remote):
	opener = urllib.FancyURLopener(proxies)
	if (not os.access(local,os.F_OK)):
		download(local,remote)
	else:
		if (not os.path.getsize(local)):
		 	download(local,remote)
		else:
			pass


def GetGVP(filename,docid):
	file = ("http://video.google.com/videogvp/%s?docid=%s" % (filename,docid))
	return file

class ToInteger(TokenConverter):
    """Converter to make token into an integer."""
    def postParse( self, instring, loc, tokenlist ):
	return int(tokenlist[0])

def GetHTTP(server,file,cachetitle,disableCache=False):
	conn = httplib.HTTPConnection(server)
	from_cache = False
	
	try: 
		os.mkdir(("./odtv_cache/"))
	except:
		pass
	if (not disableCache):
		from_cache = os.path.isfile("./odtv_cache/%s" % cachetitle)
	
	if (not from_cache):
		conn.request("GET",file)
		r1 = conn.getresponse()
		cachedata1 = r1.read()
		conn.close()
		fd = open("./odtv_cache/%s" % cachetitle,"w")
		fd.write(cachedata1)
		fd.close()
		return cachedata1
	else:
		fd = open("./odtv_cache/%s" % cachetitle,"r")
		cachedata1 = fd.read()
		fd.close()
		return cachedata1

def GenerateHTML():
	names = os.listdir("./")
	file = open("index.xml","w")
	file.write("<trailers>")
	for name in names:
		if (not os.path.isdir(name)):
			extension  = name.split(".")
			try: 
				if (extension[2]=="odtv"):
					odtv_file = open(name,"r")
					data = odtv_file.read()
					data = data.split(";")
					odtv_file.close()
					title = data[0]
					movie = data[1]
					pic = data[2]
					file.write("\t<trailer>\n")
					file.write("\t\t<title>%s</title>\n" %title)
					file.write("\t\t<pic>%s</pic>\n" %pic)
					file.write("\t\t<file>%s</file>\n" %movie)
					file.write("\t</trailer>\n")

			except:
				pass
	file.write("</trailers>")
	file.close()

data1 = GetHTTP("video.google.com","/movietrailers.html","movietrailers.html",True)
sign = oneOf("+ -")
space = Optional(" ")
id = ToInteger(Combine(Optional(sign) + Word(nums))).setResultsName("id")
name = Group(space + Word(alphas) + space + Optional(Word(alphas))).setResultsName("filename")
url = Literal("\"") + Group(Literal("/videoplay?docid=") ^ Literal("http://video.google.com/videoplay?docid=")) + id.setResultsName("docid") + Literal("\"")
links = Literal("> <a href=") + quotedString.setResultsName("link")  + Literal("><img src=\"mov")


thumbs = []
image = Literal("><img src=") + Word(printables).setResultsName("image") +  Literal("class=\"pic\"")
for ti,si,ei in image.scanString(data1):
	imgdata = ti.image
	img = imgdata.split("\"")
	thumbs.append(img[1])
	
i = -1
for t,s,e in links.scanString(data1):
	i = i + 1
	thumbfile = thumbs[i].split("/")
	tmp_link = t.link[0].split("\"")[1]	
	for t3,s3,e3 in id.scanString(tmp_link):
		t3.id
		break
	filename = "%s%s" % (thumbfile[1].split(".")[0],".gvp")
	data2 = GetHTTP("video.google.com",GetGVP(filename,t3.id),filename)
	lines = data2.splitlines()
	url = lines[2].split(":")
	try:
		duration = lines[4].split(":")
		title = lines[5].split(":")
		img = thumbfile[1]
		downloadFile(thumbfile[1],"http://video.google.com/%s" % thumbs[i])
	
		filetitle = "%s.avi" % title[1]
		print title[1]
		video = "http:%s" % url[2] 
		downloadFile(filetitle,video)
		createIndex(filetitle,img,title[1])
	except:
		pass

GenerateHTML()

